import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import learning.xml.jaxb.MusicalStyle;
import save.xml.jaxb.Interface;


public class XML {
	public static String ARTIST;
	public static String SONG;
	public static String SCORE;
	public static String STYLE;
	public static JAXBContext artistContext;
	public static JAXBContext songContext;
	public static JAXBContext styleContext;
	public static JAXBContext scoreContext;
	
	public static String INTERFACE;
	public static JAXBContext interfaceContext;
	public static Interface _interface = new Interface();

	public static String MUSICAL_STYLE;
	public static JAXBContext context; 
	public static MusicalStyle musicalStyle = new MusicalStyle();
	public static MusicalStyle musicalStyle2 = null;

	
// ----------------------- xml Interface info ------------------------------------------------------------------
	
	public void newInterface(String name) throws JAXBException, IOException {
		interfaceContext = JAXBContext.newInstance(Interface.class);
		_interface.setName(name);
		INTERFACE = ("./" + name);
	}
	
	public void setWidth(int width) {
		_interface.setWidth(width);
	}

	public void setHeight(int height) {
		_interface.setHeight(height);
	}

	public void setLocationX(int locationX) {
		_interface.setLocationX(locationX);
	}
	
	public void setLocationY(int locationY) {
		_interface.setLocationY(locationY);
	}

	public void setButtons(Object[][] buttons) {
		_interface.setButtons(buttons);
	}
	
	public void setGraphs(Object[][] graphs) {
		_interface.setGraphs(graphs);
	}
	public void setRadials(Object[][] radials) {
		_interface.setRadials(radials);
	}
	public void writeInterface() throws JAXBException, IOException {
		Marshaller m = interfaceContext.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(_interface, System.out);
		Writer w = null;
		try {
			w = new FileWriter(INTERFACE);
			m.marshal(_interface, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}

	public boolean readInterface(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		INTERFACE = ("./" + name);
		boolean exists = true;
		try {
			_interface = (Interface) um.unmarshal(new FileReader(
					INTERFACE));

		} catch (FileNotFoundException e) {
			exists = false;
		} catch (JAXBException e) {
			exists = false;
		}
		return exists;
	}
	
	public static int getWidth(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int width = _interface.getWidth();
		return width;
	}
	public static int getHeight(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int height = _interface.getHeight();
		return height;
	}
	public static int getLocationX(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int locationX = _interface.getLocationX();
		return locationX;
	}
	public static int getLocationY(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int locationY = _interface.getLocationY();
		return locationY;
	}
	public static Object[][] getButtons(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		Object[][] buttons = _interface.getButtons();
		return buttons;
	}
	public static Object[][] getGraphs(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		Object[][] graphs = _interface.getGraphs();
		return graphs;
	}
	public static Object[][] getRadials(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		Object[][] radials = _interface.getRadials();
		return radials;
		
		
	}

//	---------------------- xml Style info: ---------------------------------------------------------------------
	public void newStyle(String name) throws JAXBException, IOException {
		context = JAXBContext.newInstance(MusicalStyle.class);

		musicalStyle.setName(name);
		MUSICAL_STYLE=("./"+name+".xml");
	}
	
	public void setDuration(int[] duration){
		musicalStyle.setDuration(duration);
	}
	public void setCount(int count){
		musicalStyle.setCount(count);
	}
	public void setNotes(int[] notes){
		musicalStyle.setNotes(notes);
	}
	public void setVolume(int[] volume){
		musicalStyle.setVolume(volume);
	}
	
	public void writeStyle() throws JAXBException, IOException{
		// create JAXB context and instantiate marshaller
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(musicalStyle, System.out);
		Writer w = null;
		try {
			w = new FileWriter(MUSICAL_STYLE);
			m.marshal(musicalStyle, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}
	
	public boolean readStyle(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MUSICAL_STYLE=("./"+name+".xml");
		boolean exists=true;

		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		}
		return exists;
	}
	
	
	
	public static boolean readStyleStatic(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MUSICAL_STYLE=("./"+name+".xml");
		boolean exists=true;

		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists=false;
//			e.printStackTrace();
		}
		return exists;
	}
	
	public static int[] getNotes(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] notes = musicalStyle2.getNotes();
		return notes;
	}
	public static int[] getVolume(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] volume = musicalStyle2.getVolume();
		return volume;
	}
	
	
	public static int[] getDuration(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] duration = musicalStyle2.getDuration();
		return duration;
	}
	
	
	public static int getCount(String name) throws JAXBException{
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = context.createUnmarshaller();
		MusicalStyle musicalStyle2 = null;
		MUSICAL_STYLE=("./"+name+".xml");
		try {
			musicalStyle2 = (MusicalStyle) um.unmarshal(new FileReader(
					MUSICAL_STYLE));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int count = musicalStyle2.getCount();
		return count;
	}
	public void init() throws JAXBException, IOException {
		newStyle("default");
	}
}

